package com.ds.robot.assistant.service;

import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.ds.robot.assistant.entity.TargetAchievementEntity;
import com.ds.robot.assistant.entity.TargetOkrEntity;
import com.ds.robot.assistant.enums.MetricType;
import com.ds.robot.assistant.enums.PrincipalType;
import com.ds.robot.assistant.vo.resp.DecimalChartData;
import com.ds.robot.assistant.vo.resp.SeriesData;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
 * 作用：
 *
 * @author WeiShaoying
 * 
 */
@Component("metricService")
@Slf4j
public class MetricService {

    @Autowired
    private TargetAchievementService targetAchievementService;

    @Autowired
    private TargetOkrService targetOkrService;

    private Map<Integer, String> metricMap;

    private final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

    public static final String DATE = "2024-10-16";

    @PostConstruct
    public void init() {
        List<TargetOkrEntity> list = targetOkrService.list();
        if (!list.isEmpty()) {
            metricMap = list.stream().collect(Collectors.toMap(TargetOkrEntity::getId, TargetOkrEntity::getTargetName));
        }
    }

    public List<SeriesData> queryCoreMetricData(MetricType metricType) {
        List<Integer> metricList;
        switch (metricType) {
            case CLIENT_APP:
                metricList = Collections.singletonList(6);
                break;
            case FRANCHISEE_PERFORMANCE:
                metricList = Collections.singletonList(7);
                break;
            case TOOL_USAGE_COUNT:
                metricList = Arrays.asList(8, 9);
                break;
            case CLOUD_WAREHOUSE_COVERAGE:
                metricList = Collections.singletonList(10);
                break;
            case CLIENT:
            default:
                metricList = Arrays.asList(4, 5);
                break;
        }
        LambdaQueryChainWrapper<TargetAchievementEntity> wrapper = targetAchievementService.lambdaQuery().in(TargetAchievementEntity::getTargetTypeId, metricList);
        if (MetricType.TOOL_USAGE_COUNT.equals(metricType)) {
            try {
                wrapper.ge(TargetAchievementEntity::getAchievementDate, sdf.parse(DATE));
            } catch (ParseException e) {
                log.error("解析日期错误", e);
            }
        }
        List<TargetAchievementEntity> entityList = wrapper.list();
        if (entityList == null || entityList.isEmpty()) {
            return Collections.emptyList();
        }
        Map<Integer, Map<PrincipalType, Map<Date, List<TargetAchievementEntity>>>> mapMap = entityList.stream().collect(
                Collectors.groupingBy(TargetAchievementEntity::getTargetTypeId,
                        Collectors.groupingBy(TargetAchievementEntity::getPrincipalType, Collectors.groupingBy(TargetAchievementEntity::getAchievementDate))
                ));

        List<SeriesData> list = new ArrayList<>();
        for (Map.Entry<Integer, Map<PrincipalType, Map<Date, List<TargetAchievementEntity>>>> entry : mapMap.entrySet()) {
            // 指标类型ID
            Integer key = entry.getKey();
            for (Map.Entry<PrincipalType, Map<Date, List<TargetAchievementEntity>>> mapEntry : entry.getValue().entrySet()) {
                PrincipalType principalType = mapEntry.getKey();
                // 主体类型
                String metricName = metricMap.get(key) + "-" + principalType.getDesc();
                // 系列数据
                SeriesData seriesData = new SeriesData();
                seriesData.setName(metricName);
                List<DecimalChartData> dataList = mapEntry.getValue().entrySet().stream().map(element -> {
                            DecimalChartData decimalChartData = new DecimalChartData();
                            decimalChartData.setDate(sdf.format(element.getKey()));
                            decimalChartData.setData(element.getValue().get(0).getValue());
                            return decimalChartData;
                        })
                        //按照 date升序排列
                        .sorted(Comparator.comparing(DecimalChartData::getDate))
                        .collect(Collectors.toList());
                seriesData.setData(dataList);
                list.add(seriesData);
            }
        }
        return list;
    }

}
